package com.basaji.dao.impl;

import java.util.List;

import com.basaji.dao.ITopicDao;
import com.basaji.domain.pojo.Topic;

public class TopicDao extends BaseDao implements ITopicDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getTopics(int classificationId, int start, int size)
			throws Exception {

		return pojoManager
				.getList(
						"from Topic where parentId = 0 and (classification.id = ? or classification.parentId = ?) order by replied desc",
						new Object[] { classificationId, classificationId },
						start, size);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getTopicsByItem(int itemId, int start, int size)
			throws Exception {
		return pojoManager
				.getList(
						"from Topic where itemId = ? order by replied desc",
						new Object[] { itemId },
						start, size);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Topic> getUserTopics(int uid, int start, int size)
			throws Exception {
		return pojoManager
				.getList(
						"from Topic where uid = ? and parentId = 0 order by id desc",
						new Object[] { uid},
						start, size);
	}

	@Override
	public int getTopicCount(int classificationId) throws Exception {
		return pojoManager
				.getIntCount(
						"select count(id) from Topic where parentId = 0 and (classification.id = ? or classification.parentId = ?) order by replied desc",
						new Object[] { classificationId, classificationId });
	}

	@Override
	public int getTopicCountByItem(int itemId) throws Exception {
		return pojoManager
				.getIntCount(
						"select count(id) from Topic where itemId = ?) order by replied desc",
						new Object[] { itemId });
	}

	@Override
	public int getUserTopicCount(int uid) throws Exception {
		return pojoManager
				.getIntCount(
						"select count(id) from Topic where uid = ? and parentId=0) order by replied desc",
						new Object[] { uid });
	}

	@Override
	public List<Topic> getReplies(int id, int start, int size) throws Exception {
		return pojoManager
				.getList(
						"from Topic where parentId = ? order by replied desc",
						new Object[] { id },
						start, size);
	}

}
